package org.jdcenter.jndi.context.remote;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.naming.Name;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdcenter.jndi.context.StubContext;

public class RMIJRMPContextClient extends RMIContextClient{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 402942259586866504L;

	private Log log = LogFactory.getLog(getClass());
	
	private StubContext stubContext = null;
	
	public RMIJRMPContextClient(String ip,int port){
		super(ip,port);
	}

	public void start() throws NamingException{
		try {
			log.debug(url);
			stubContext = (StubContext)Naming.lookup(url);
			log.info(this+" running");
		} catch (MalformedURLException e) {
			log.error(e);
            throw new NamingException(e.getMessage());
		} catch (RemoteException e) {
			log.error(e);
            throw new NamingException(e.getMessage());
		} catch (NotBoundException e) {
			log.error(e);
            throw new NamingException(e.getMessage());
		}
	}
	public void stop() throws NamingException{
		stubContext = null;
		log.info(this+" closed");
	}
	

	public void bind(Name name, Object obj) throws NamingException {
		stubContext.bind(name, obj);
    }
    
    public void rebind(Name name, Object obj) throws NamingException {
    	stubContext.rebind(name, obj);
    }
    
    public void unbind(Name name) throws NamingException {
    	stubContext.unbind(name);
    }
    
    public Object lookup(Name name) throws NamingException {
    	return stubContext.lookup(name);
    }
	
}
